/*-
 * #%L
 * HAPI FHIR - Docs
 * %%
 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package ca.uhn.hapi.fhir.docs.interceptor;

import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Pointcut;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.HumanName;
import org.hl7.fhir.r4.model.Patient;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;

// START SNIPPET: patientInterceptor
/**
 * This is a simple interceptor that will remove a humanName when it is found to be
 * black listed.
 */
public class PatientNameModifierMdmPreProcessingInterceptor {
	List<String> myNamesToIgnore = asList("John Doe", "Jane Doe");

	@Hook(Pointcut.MDM_BEFORE_PERSISTED_RESOURCE_CHECKED)
	public void invoke(IBaseResource theResource) {

		Patient patient = (Patient) theResource;
		List<HumanName> nameList = patient.getName();

		List<HumanName> validHumanNameList = nameList.stream()
				.filter(theHumanName -> !myNamesToIgnore.contains(theHumanName.getNameAsSingleString()))
				.collect(Collectors.toList());

		patient.setName(validHumanNameList);
	}
}

// END SNIPPET: patientInterceptor
